package com.xinzhi.savingpot.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.xinzhi.savingpot.userinfo.entity.UserInfo;

import java.util.Date;


public class JwtUtils {
    public static final Long EXPIRE_TIME = Long.valueOf(1000 * 60 *60);  //过期时间，毫秒，一秒

    /**
     * 生成jwt
     */
    public static String createJsonWebToken(UserInfo ui) {

        String token = "";
        Date date = new Date();
        date.setTime(System.currentTimeMillis() + EXPIRE_TIME);
        token = JWT.create().withAudience(String.valueOf(ui.getUserId()))
                .withClaim("name",ui.getUserNickName())
                .withExpiresAt(date)
                .sign(Algorithm.HMAC256(ui.getPassword()));//使用HMAC256生成token，密钥是用户的密码
        return token;
    }

}
